package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.MyAccontVm;
import com.typy.www.tpay.bean.vm.UserInfoVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/7/14.
 */

public class FabuActivity extends BaseBackActivity {


    @BindView(R.id.btn_titlebar_left)
    ImageButton btnTitlebarLeft;
    @BindView(R.id.btn_titlebar_right)
    Button btnTitlebarRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_zichan_title)
    TextView tvZichanTitle;
    @BindView(R.id.tv_zichan)
    TextView tvZichan;
    @BindView(R.id.tv_yue_title)
    TextView tvYueTitle;
    @BindView(R.id.tv_yue_detail)
    TextView tvYueDetail;
    @BindView(R.id.et_chikaren)
    AutoCompleteTextView etChikaren;
    @BindView(R.id.et_chushoujiages)
    AutoCompleteTextView etChushoujiages;
    @BindView(R.id.et_chushoushuliangs)
    AutoCompleteTextView etChushoushuliangs;
    @BindView(R.id.et_zhihang)
    AutoCompleteTextView etZhihang;
    @BindView(R.id.publish)
    TextView publish;
    @BindView(R.id.sb_price)
    SeekBar sbPrice;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.tv_chushoujiage)
    TextView tvChushoujiage;
    @BindView(R.id.tv_chushoushulian)
    TextView tvChushoushulian;
    @BindView(R.id.tv_chushouzonge)
    TextView tvChushouzonge;
    private int classType;
    private String price;
    private String tpyeName;
    private String strPtr = "";
    private String strPtr2 = "0";
    UserInfoVm userInfoVm;

    public static void show(Context context, int classType, String price, String tpyeName) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        intent.putExtra("price", price);
        intent.putExtra("tpyeName", tpyeName);
        IntentUtil.intent2Activity(context, intent, FabuActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("classType", 0);
        price = getIntent().getStringExtra("price");
        tpyeName = getIntent().getStringExtra("tpyeName");
        updataView();
    }

    private void updataView() {
        publish.setText("发布");
        UpdataUiUtil.getDefaul().setText(ClassHoldUtil.getClassTitle(classType), tvTitle)
                .setText(price+"", etChikaren).setText(tpyeName + "资产", tvZichanTitle);
        if (ClassHoldUtil.FABUSELL == classType) {
            UpdataUiUtil.getDefaul().setText("出售价格", tvChushoujiage)
                    .setText("出售数量", tvChushoushulian).setText("出售总额", tvChushouzonge);
        }else{
            UpdataUiUtil.getDefaul().setText("购买价格", tvChushoujiage)
                    .setText("购买数量", tvChushoushulian).setText("购买总额" , tvChushouzonge);
        }
        seekBarControl(0);
        etZhihang.setText("0");
        btnTitlebarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        refresh();
        etChushoushuliangs.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtil.isEmpty(charSequence.toString()))
                    strPtr2 = charSequence.toString();
                else
                    strPtr2 = "0";
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!StringUtil.isEmpty(editable.toString()) && !strPtr2.equals(editable.toString())) {
                    strPtr2 = editable.toString();
                    double totalprice = Double.valueOf(strPtr) * Double.valueOf(strPtr2);
                    etZhihang.setText(totalprice + "");
                } else {
                    etZhihang.setText(0 + "");
                }
            }
        });
        sbPrice.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                seekBarControl(i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void seekBarControl(int progress) {
        double rate = 0;
        if (ClassHoldUtil.FABUSELL == classType) {
            rate = (progress / 10 + 100) * 100 / 100;
        } else {
            rate = (progress / 5 + 90) * 100 / 100;
        }
        tvProgress.setText(rate + "%");
        double jiage = Double.valueOf(price) * rate / 100;
        double lastJiage = Double.valueOf(StringUtil.subZeroAndDot(jiage, 4));
        etChushoujiages.setText(StringUtil.subZeroAndDot(jiage, 4));
        String shulian = etChushoushuliangs.getText().toString();
        if (!StringUtil.isEmpty(shulian)) {
            double lastAmount = lastJiage * Double.valueOf(shulian);
            etZhihang.setText(StringUtil.subZeroAndDot(lastAmount, 4));
        } else {
            etZhihang.setText("0");
        }
        strPtr = jiage + "";
    }


    @Override
    protected int getTitleBarResId() {
        return -1;
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_fabu;
    }


    @OnClick(R.id.publish)
    public void onViewClicked() {
        if (ClickFastUtil.isFastClick1000() && CheckUtil.checkText(getActivity(), etChushoujiages, etChushoushuliangs)) {
            PwdDailogUtil.show(this, new InputCallBack() {
                @Override
                public void onInputFinish(String result) {
                    getSavebuyorderData(result);
                }
            });
        }
    }

    private void refresh() {
        getUserData();
        getMyaccountData();
    }


    //获取个人信息
    private void getUserData() {
        ParamsUtil params = new ParamsUtil();
        HTTPUtils.post(this, Constance.getMyselfinfoURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                userInfoVm = GsonUtils.parseJSON(ret.getData().toString(), UserInfoVm.class);
                if(userInfoVm!=null)
                UpdataUiUtil.getDefaul().setText(userInfoVm.getFdAmount(), tvYueDetail);
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void getMyaccountData() {
        ParamsUtil paramsUtil = new ParamsUtil();
        HTTPUtils.post(getActivity(), Constance.getMyaccountURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                MyAccontVm vm = GsonUtils.parseJSON(ret.getData().toString(), MyAccontVm.class);
                if (vm != null) {
                    UpdataUiUtil.getDefaul().setText(vm.getFdIconCount(), tvZichan);
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
            }
        });
    }

    private void getSavebuyorderData(String pwd) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdIconTypeName", tpyeName);
        paramsUtil.put("fdPrice", etChushoujiages);
        paramsUtil.put("fdQuantity", etChushoushuliangs);
        paramsUtil.putMd5("fdPayPwd", pwd);

        String url = "";
        if (ClassHoldUtil.FABUBUY == classType) {
            url = Constance.getSavebuyorderURL();
        } else {
            url = Constance.getSavesellerorderURL();
        }
        HTTPUtils.post(getActivity(), url, paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "发布成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.DIGIDINGDANG);
                        EventBus.getDefault().post(new MessageEvent(MessageAction.FEFRESHDITAIL));
                        finish();
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", msg, "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {

                    }
                });
            }
        });
    }
}
